<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="dialogAudit.visible"
    :width="900"
    :title="dialogAudit.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog(false)"
  >
    <el-descriptions v-loading="loading" :column="2" border>
      <el-descriptions-item label="项目名称">{{ formData.projectName }}</el-descriptions-item>
      <el-descriptions-item label="流水号">{{ formData.code }}</el-descriptions-item>
      <el-descriptions-item label="开户银行">{{ formData.bankType }}</el-descriptions-item>
      <el-descriptions-item label="支行名称">{{ formData.bankSubbranch }} </el-descriptions-item>
      <el-descriptions-item label="开户提交人">{{ formData.creator }} </el-descriptions-item>
      <el-descriptions-item label="开户申请日期">{{ formData.createTime }} </el-descriptions-item>
    </el-descriptions>
    <el-form
      ref="addOrEditFormRef"
      v-loading="loading"
      :model="formData"
      :rules="rules"
      label-width="180"
      size=""
      style="padding-top: 20px"
    >
      <el-row justify="start" class="d_left">
        <el-col :span="12" class="">
          <el-form-item label="账户名称" prop="username">
            <el-input v-model="formData.username" placeholder="请输入账户名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="账号" prop="bankNo">
            <el-input v-model="formData.bankNo" placeholder="请输入账号" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="开户银行" prop="bankType">
            <el-select v-model="formData.bankType" filterable clearable @change="changeBankType">
              <el-option v-for="item in bankTypeTypes" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="银行编码" prop="bankType">
            <el-input v-model="formData.bankType" placeholder="请输入银行编码" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="建设单位名称" prop="constructorName">
            <el-input v-model="formData.constructorName" disabled placeholder="请输入建设单位名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="建设单位审核人" prop="constructorChecker">
            <el-input v-model="formData.constructorChecker" placeholder="请输入建设单位审核人" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="建设单位审核人电话" prop="constructorMobile">
            <el-input v-model="formData.constructorMobile" placeholder="请输入建设单位审核人电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="建设单位审核人身份证号" prop="constructorCardNo">
            <el-input v-model="formData.constructorCardNo" placeholder="请输入建设单位审核人身份证号" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="施工总承包名称" prop="lumpContractorCorpName">
            <el-input v-model="formData.lumpContractorCorpName" disabled placeholder="请输入施工总承包名称" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="">
          <el-form-item label="施工总承包审核人" prop="lumpContractorCorpChecker">
            <el-input v-model="formData.lumpContractorCorpChecker" placeholder="请输入施工总承包审核人" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="施工总承包审核人电话" prop="lumpContractorCorpMobile">
            <el-input v-model="formData.lumpContractorCorpMobile" placeholder="请输入施工总承包审核人电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="施工总承包审核人身份证号" prop="lumpContractorCorpCardNo">
            <el-input v-model="formData.lumpContractorCorpCardNo" placeholder="请输入施工总承包审核人身份证号" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogAudit.footBtn }}</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ProjectSpecialAccountAuditForm } from "@/api/specialaccount/types";
import {
  AuditProjectSpecialAccount,
  GetProjectSpecialAccount,
  UpdateAuditProjectSpecialAccount,
} from "@/api/specialaccount/index";
import { getDicts } from "@/store/modules/dict";
import { FormRules } from "element-plus";

const props = defineProps({
  dialogAudit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "开户审批",
      footBtn: "确认提交",
      id: 0,
    }),
  },
});

const { dialogAudit } = toRefs(props);
const bankTypeTypes = getDicts("bankTypeTypes"); //银行

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
const formData = reactive<ProjectSpecialAccountAuditForm>({});
const addOrEditFormRef = ref(ElForm);

const rules = reactive<FormRules<ProjectSpecialAccountAuditForm>>({
  username: [{ required: true, message: "请输入账户名称", trigger: "blur" }],
  bankNo: [{ required: true, message: "请输入银行账号", trigger: "blur" }],
  bankType: [{ required: true, message: "请选择银行类型", trigger: "blur" }],
  constructorChecker: [{ required: true, message: "请输入建设单位审核人", trigger: "blur" }],
  constructorCardNo: [{ required: true, message: "请输入建设单位审核人身份证号", trigger: "blur" }],
  lumpContractorCorpChecker: [{ required: true, message: "请输入施工总承包审核人", trigger: "blur" }],
  constructorMobile: [
    { required: true, message: "请输入建设单位审核人手机号", trigger: "blur" },
    { pattern: /^1[345678]\d{9}$/, message: "请输入正确的手机号", trigger: "blur" },
  ],
  lumpContractorCorpMobile: [
    { required: true, message: "请输入施工总承包审核人手机号", trigger: "blur" },
    { pattern: /^1[345678]\d{9}$/, message: "请输入正确的手机号", trigger: "blur" },
  ],
  lumpContractorCorpCardNo: [{ required: true, message: "请输入施工总承包审核人身份证号", trigger: "blur" }],
});

const changeBankType = (value: any) => {
  var temp = bankTypeTypes.find((item: any) => item.value == value);
  if (temp) {
    formData.bankType = temp.value;
    formData.bankName = temp.label;
    return;
  }
  formData.bankType = undefined;
  formData.bankName = undefined;
};

onMounted(() => {
  loading.value = true;
  GetProjectSpecialAccount(dialogAudit.value.id)
    .then((res) => {
      if (res.type == 0) {
        if (res.success) {
          if (res.username == "") {
            res.data.username = res.data.lumpContractorCorpName;
          }
          Object.assign(formData, res.data);
        } else {
        }
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
});

/**
 * 关闭弹窗
 */
function closeDialog(query: boolean) {
  emit("sendClose", query);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();
  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      if (formData.accountStatus != 1) {
        UpdateAuditProjectSpecialAccount(formData)
          .then((res) => {
            if (res.type === 20) {
              let errorMsg = "";
              res.data.forEach((item: any) => {
                errorMsg += item + "\n";
              });
              ElMessage.error(errorMsg);
            }
            if (res.type === 0) {
              if (res.success) {
                closeDialog(true);
              } else {
              }
            } else {
            }
          })
          .finally(() => (loading.value = false));
        return;
      }
      AuditProjectSpecialAccount(formData)
        .then((res) => {
          if (res.type == 0) {
            if (res.success) {
              closeDialog(true);
            } else {
            }
          } else {
          }
        })
        .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 10px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}
.dialog_footer {
  // display: flex;
  // justify-content: center;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
